#!/usr/bin/env bash

source ./init.sh

mc_install() {
	apt-get install memcached -y
	mc_mk_config
}

mc_mk_config() {
	local savefile=/etc/memcached.conf
	if [ -f ${savefile} ]; then
		rm ${savefile} -f
	fi

	local tpl=${CUR_DIR}/memcached/memcached.conf.tpl
	IFS=""
	local data=$(read_file ${tpl})

	data=${data//\#LOG_FILE\#/${MC_LOG_FILE}}
	data=${data//\#MEM_SIZE\#/${MC_SIZE}}
	data=${data//\#PORT\#/${MC_PORT}}
	data=${data//\#HOST\#/${MC_HOST}}
	data=${data//\#USER\#/${MC_USER}}

	echo ${data} > ${savefile}

	if [ -f ${savefile} ]; then
		echo "Create ${savefile} success!"
		return 0
	else
		echo "Create ${savefile} lost!"
		return 1
	fi
}

mcr_double_conv_install() {
	## first double conv
	local workdir=${PREFIX_HOME}
	cd ${workdir}
	if [ ! -d /usr/include/double-conversion ]; then
		git clone ${MCR_DOUBLE_CONV_SOURCE}
		cd "${workdir}/double-conversion"
		scons prefix="${MCR_DOUBLE_CONV_INSTALL_DIR}" install

		# Folly looks for double-conversion/double-conversion.h
		ln -sf src double-conversion

		export LDFLAGS="-L${MCR_DOUBLE_CONV_INSTALL_DIR}/lib -L${workdir}/double-conversion -ldl"
		export CPPFLAGS="-I${MCR_DOUBLE_CONV_INSTALL_DIR}/include -I${workdir}/double-conversion"
	fi
}

folly_build() {
	local rebuild=0
	local workdir=${PREFIX_HOME}/facebook
	local PKG_DIR=${workdir}
	local INSTALL_DIR=${PREFIX_HOME}/double-conversion
	local needclean=0
	if [ ! -d ${workdir} ]; then
		mkdir ${workdir}
	fi
	cd ${workdir}

	if [ -n "${1}" ]; then
		rebuild=1
	fi

	if [[ ${rebuild} -gt 0 || ! -d /usr/local/include/folly ]]; then
		apt-get install -y ${MCR_FOLLY_REQ_PKGS}
	fi

	if [ -d "${workdir}/folly" ]; then
		needclean=1
	else
		git clone ${MCR_FOLLY_SOURCE}
	fi

	if [ ! -d /usr/include/double-conversion ]; then
		git clone ${MCR_DOUBLE_CONV_SOURCE}
		cd "$PKG_DIR/double-conversion/"
		scons prefix="$INSTALL_DIR" install

		# Folly looks for double-conversion/double-conversion.h
		ln -sf src double-conversion

		export LDFLAGS="-L$INSTALL_DIR/lib -L$PKG_DIR/double-conversion -ldl"
		export CPPFLAGS="-I$INSTALL_DIR/include -I$PKG_DIR/double-conversion"
	fi

	if [[ ${rebuild} -gt 0 || ! -d /usr/local/include/folly ]]; then
		cd "$PKG_DIR/folly/folly/"
		if [ ${needclean} -gt 0 ]; then
			make clean
		fi
		autoreconf -ivf
		./configure
		make
		make install
	fi
}

mcr_fb_install() {
	if [ ! -n "${MCR_POOL_SERVERS}" ]; then
		echo "Undefined MCR_POOL_SERVERS!"
		return 1
	fi
	local rebuild=0
	local workdir=${PREFIX_HOME}/facebook
	local installdir=${PREFIX_HOME}/mcrouter

	if [ ! -d ${workdir} ]; then
		mkdir ${workdir}
	fi
	cd ${workdir}

	if [ -n "${1}" ]; then
		rebuild=1
	fi

	if [ ! -d "${workdir}/mcrouter" ]; then
		git clone ${MCR_SOURCE}
	fi

	if [[ ${rebuild} -gt 0 || ! -f /usr/local/bin/mcrouter ]]; then
		cd "${workdir}/mcrouter/mcrouter"
		./scripts/install_ubuntu_14.04.sh ${installdir}
	fi

	if [ ! -f /usr/local/bin/mcrouter ]; then
		echo "facebook/msrouter install fail! Please check!"
	else
		mcr_mk_config
		mcr_mk_script
	fi
}

mcr_install() {
	if [ ! -n "${MCR_POOL_SERVERS}" ]; then
		echo "Undefined MCR_POOL_SERVERS!"
		return 1
	fi
	local rebuild=0
	local workdir=${PREFIX_HOME}/facebook
	local PKG_DIR=${workdir}
	local INSTALL_DIR=${PREFIX_HOME}/double-conversion
	if [ ! -d ${workdir} ]; then
		mkdir ${workdir}
	fi
	cd ${workdir}

	if [ -n "${1}" ]; then
		rebuild=1
	fi

	folly_build $1

	if [[ ${rebuild} -gt 0 || ! -f /usr/local/bin/mcrouter ]]; then
		apt-get install -y ${MCR_REQ_PKGS}

		cd "$PKG_DIR/mcrouter/mcrouter/"
		autoreconf --install
		./configure
		make
		make install
	fi

	if [ ! -f /usr/local/bin/mcrouter ]; then
		echo "facebook/msrouter install fail! Please check!"
	else
		mcr_mk_config
		mcr_mk_script
	fi
}

mcr_mk_config() {
	if [ ! -n "${MCR_POOL_SERVERS}" ]; then
		echo "Undefined MCR_POOL_SERVERS!"
		return 1
	fi
	local savefile=${MCR_CONF_FILE}
	local tpl=${CUR_DIR}/memcached/mcrouter.json.tpl
	IFS=""
	local data=$(read_file ${tpl})
	data=${data//\#SERVERS\#/${MCR_POOL_SERVERS}}

	echo ${data} > ${savefile}

	if [ -f ${savefile} ]; then
		echo "Create ${savefile} success!"
		return 0
	else
		echo "Create ${savefile} lost!"
		return 1
	fi
}

mcr_mk_script() {
	local savefile=/etc/init.d/mcrouter
	local tpl=${CUR_DIR}/memcached/mcrouter.sh.tpl
	IFS=""
	local data=$(read_file ${tpl})
	data=${data//\#PORT\#/${MCR_PORT}}
	data=${data//\#CONF_FILE\#/${MCR_CONF_FILE}}
	data=${data//\#LOG_FILE\#/${MCR_LOG_FILE}}
	data=${data//\#PID_FILE\#/${MCR_PID_FILE}}

	echo ${data} > ${savefile}
	chmod +x ${savefile}

	if [ -f ${savefile} ]; then
		echo "Create ${savefile} success!"
		return 0
	else
		echo "Create ${savefile} lost!"
		return 1
	fi
}

case "$1" in
	install)
		mc_install
		;;
	conf)
		mc_mk_config
		;;
	mcr-install)
		mcr_install
		;;
	mcr-fb-build)
		mcr_fb_install $2
		;;
	mcr-build)
		mcr_install $2
		;;
	mcr-conf)
		mcr_mk_config
		;;
	mcr-script|mcr-sh)
		mcr_mk_script
		;;
	*)
		echo "./mc.sh {install|conf|mcr-install|mcr-fb-build|mcr-build|mcr-conf|mcr-script|mcr-sh}"
		;;
esac

on_tail